package com.wwlwxg.filemanager.web.controller;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import com.wwlwxg.filemanager.modules.contant.DefaultValues;
import com.wwlwxg.filemanager.service.IFileManagerService;
import com.wwlwxg.filemanager.util.ControllerUtils;
import com.zhazhapan.util.ArrayUtils;
import org.quartz.SimpleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author: coco
 * @time 2019/2/3 - 9:30
 */
@RestController
@RequestMapping("/filemanager")
@Configuration
public class FileManagerController {

    private final IFileManagerService fileManagerService;

    private final JSONObject jsonObject;

    @Value("${project.basePath}")
    private String basePath;

    @Autowired
    public FileManagerController(IFileManagerService fileManagerService, JSONObject jsonObject) {
        this.fileManagerService = fileManagerService;
        this.jsonObject = jsonObject;
    }

    @PostMapping("/list")
    public String list(@RequestBody JSONObject jsonObject) {
        System.out.println("------------------>" + jsonObject.toJSONString());
        jsonObject.put("result", fileManagerService.list(jsonObject));
        return jsonObject.toJSONString();
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response, String path) throws IOException {
        System.out.println("-================");

        //ControllerUtils.loadResource(response, DefaultValues.basePath + path, true);
        ControllerUtils.loadResource(response, basePath + path, true);
    }

    @GetMapping("/multidownload")
    public void multiDownload(HttpServletResponse response, String[] items, String toFilename) throws IOException {
        ControllerUtils.setResponseFileName(response, toFilename);
        fileManagerService.multiDownload(response, items, toFilename);
    }

    @PostMapping("/upload")
    public String upload(String destination, MultipartHttpServletRequest request) {
        Map<String, MultipartFile> fileMap = request.getFileMap();
        MultipartFile[] files = ArrayUtils.mapToArray(fileMap, MultipartFile.class);
        jsonObject.put("result", fileManagerService.upload(destination, files));
        return jsonObject.toJSONString();
    }

    @PostMapping("/rename")
    public String rename(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.rename(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/copy")
    public String copy(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.copy(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/move")
    public String move(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.move(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/remove")
    public String remove(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.remove(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/folder")
    public String folder(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.createFolder(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/content")
    public String content(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.getContent(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/edit")
    public String edit(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.edit(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/compress")
    public String compress(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.compress(json));
        return jsonObject.toJSONString();
    }

    @PostMapping("/extract")
    public String extract(@RequestBody JSONObject json) {
        jsonObject.put("result", fileManagerService.extract(json));
        return jsonObject.toJSONString();
    }
}
